﻿/**********************************************************************************
 * Description:     
 * MemberVariable:
 * Functions:       
 * Modify information:     
 * 2008-8-5     New           DuYaoHui     Create Class
 * 
 * ********************************************************************************/
using System;
using System.Data;
using System.Web.UI.WebControls;
using Fjaspx.Common;
using System.IO;
using System.Xml;

namespace Fjaspx.UI.Web
{
    /// <summary>
    /// 插入对象类
    /// </summary>
    public partial class User_Objet__add : UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string MexBoxContent = Fjaspx.Common.SystemCms.Read_File(Server.MapPath("~/administrator/SystemTemplate/MexBox.htm"), "utf-8");
                if (Request["code"] != null && Request["code"].ToUpperInvariant() == Fjaspx.Common.Session.Get("code").ToUpperInvariant())
                {
                    int NewId = add();
                    if (NewId!=1)
                    {
                        if (Fjaspx.Common.Session.Get("user_id") != null)
                        {
                            ___ROYcms_New_User_model.New_id = NewId;
                            ___ROYcms_New_User_model.User_id = Convert.ToInt32(Fjaspx.Common.Session.Get("user_id"));
                        }

                        ___ROYcms_New_User_bll.Add(___ROYcms_New_User_model);
                        MexBoxContent = MexBoxContent.Replace("{$IndexPage}", ___WebConfig.web_host);
                        MexBoxContent = MexBoxContent.Replace("{$message}", "录入成功！");
                        Response.Write(MexBoxContent);
                    }
                    else {
                        MexBoxContent = MexBoxContent.Replace("{$IndexPage}", ___WebConfig.web_host);
                        MexBoxContent = MexBoxContent.Replace("{$message}", "错误！");
                        Response.Write(MexBoxContent);
                    }
                }
                else {
                    MexBoxContent = MexBoxContent.Replace("{$IndexPage}", ___WebConfig.web_host);
                    MexBoxContent = MexBoxContent.Replace("{$message}", "验证码错误！");
                    Response.Write(MexBoxContent);
                }
            }
        }
        /// <summary>
        /// 添加信息方法
        /// </summary>
        public int add()
        {
            int ID = 1;
            if (Request["title"] != null && Request["classname"] != null && Request["ClassKind"]!=null)
            {
                ___ROYcms_news_Model.pic = Request["pic"];
                ___ROYcms_news_Model.title = Request["title"];
                ___ROYcms_news_Model.keyword = Request["keyword"];
                ___ROYcms_news_Model.zhaiyao = Request["zhaiyao"];
                ___ROYcms_news_Model.classname = Convert.ToInt32(Request["classname"]);
                ___ROYcms_news_Model.contents = Request["contents"];
                ___ROYcms_news_Model.infor = Request["infor"];
                ___ROYcms_news_Model.jumpurl = Request["jumpurl"];
                ___ROYcms_news_Model.author = Request["author"];
                ___ROYcms_news_Model.url = Request["url"];
                ___ROYcms_news_Model.tag = Request["tag"];
                ___ROYcms_news_Model.orders = 0;
                ___ROYcms_news_Model.ding = 0;
                ___ROYcms_news_Model.tuijian = 1;
                ___ROYcms_news_Model.dig = 0;
                ___ROYcms_news_Model.hits = 0;
                ___ROYcms_news_Model.switchs = 1;
                ___ROYcms_news_Model.link = Request["link"];
                ___ROYcms_news_Model.type = Request["ClassKind"];
                ___ROYcms_news_Model.GUID = Request["GUID"];

                ID = ___ROYcms_news_Bll.Add(___ROYcms_news_Model);
                if (ID != 1) { NewXml(ID); }
            }
            return ID;
        }
        /// <summary>
        /// 创建XML 数据文件
        /// </summary>
        /// <param name="ID">The ID.</param>
        /// <returns></returns>
        public bool NewXml(int ID)
        {
            
            try
            {
                XmlDocument doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", ___WebConfig.templet_language, null);
                doc.AppendChild(dec);
                //创建一个根节点（一级） 
                XmlElement root = doc.CreateElement("Fjaspx");
                doc.AppendChild(root);
                //创建节点（二级） 
                XmlNode node = doc.CreateElement("News");
                XmlAttribute attr = null;
                attr = doc.CreateAttribute("Time");
                attr.Value = DateTime.Now.ToString();
                node.Attributes.SetNamedItem(attr);
                for (int i = 0; i < Request.Form.Count; i++)
                {
                    //带有"<![CDATA[   ]]>"的元素 
                    if (!Request.Form.GetKey(i).Contains("_"))
                    {
                        XmlElement element = doc.CreateElement(Request.Form.GetKey(i));
                        XmlCDataSection idata = doc.CreateCDataSection("item");
                        idata.InnerText = Request.Form.Get(i);
                        element.AppendChild(idata);
                        node.AppendChild(element);
                    }
                }
                root.AppendChild(node);
                if (!Directory.Exists(Server.MapPath("~/APP_XML/" + Request["ClassKind"] + "/"))) 
                {
                    Directory.CreateDirectory(Server.MapPath("~/APP_XML/" + Request["ClassKind"] + "/")); //创建新路径 
                }
                doc.Save(Server.MapPath("~/APP_XML/" + Request["ClassKind"] + "/" + ID + ".xml"));
                Console.Write(doc.OuterXml);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}